package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.DyEquipmentDao;
import com.sls.nzdapplication.model.DyEquipment;
import com.sls.nzdapplication.model.Equipment;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DyEquipmentUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private com.sls.nzdapplication.model.Equipment Equipment;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public DyEquipmentUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertDyEquipment(DyEquipment equipment) {
        equipment.setCreateBy(username);
        equipment.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(equipment);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultDyEquipment(final List<DyEquipment> equipments) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (DyEquipment equipment : equipments) {
                    equipment.setCreateBy(username);
                    equipment.setCreateTime(new Date());
                    manager.getDaoSession().insertOrReplace(equipment);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultDyEquipment2(final List<DyEquipment> equipments) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (DyEquipment equipment : equipments) {
                    manager.getDaoSession().insertOrReplace(equipment);
                }
            }
        });
    }

    /**
     * 删除数据
     */
    public void deleteDyEquipment(DyEquipment equipment) {
        manager.getDaoSession().delete(equipment);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateDyEquipment(DyEquipment equipment) {
        equipment.setModifyBy(username);
        equipment.setModifyTime(new Date());
        manager.getDaoSession().update(equipment);
    }

    /**
     * 按照主键返回单条数据
     */
    /**
     * 根据指定的键值查询并返回一个DyEquipment对象。
     *
     * @param key 用于查询DyEquipment对象的键值。
     * @return 返回查询到的DyEquipment对象，如果没有找到则返回null。
     */
    public DyEquipment listOneDyEquipment(String key) {
        // 通过manager获取DaoSession，然后使用该session根据键值加载DyEquipment对象
        return manager.getDaoSession().load(DyEquipment.class, key);
    }

    public List<DyEquipment> queryDyEquipmentByAreaId(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(DyEquipmentDao.Properties.AreaId.eq(id)).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndexqIdAndType(String id,String exqId,String type){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.PreviousEquipmentId.eq(exqId),DyEquipmentDao.Properties.Type.eq(type))).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndByq(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq("变压器"))).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndByq2(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),builder.or(DyEquipmentDao.Properties.Type.eq("变压器"),DyEquipmentDao.Properties.Type.eq("配电箱")))).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }


    public List<DyEquipment> queryDyEquipmentByAreaIdAscTime(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(DyEquipmentDao.Properties.AreaId.eq(id)).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdNoself(String id,String eid){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Id.notEq(eid))).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdIngd(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Id.notEq(id))).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndName(String id,String name){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Name.eq(name))).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndType(String id,String type){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq(type))).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndDY(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq("低压"))).orderDesc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndDY2(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq("低压"))).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndQzj(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq("墙支架"))).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndfzx(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq("分支箱"))).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndGD(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),DyEquipmentDao.Properties.Type.eq("拐点"))).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public List<DyEquipment> queryDyEquipmentByAreaIdAndDB(String id){
        QueryBuilder<DyEquipment> builder = manager.getDaoSession().queryBuilder(DyEquipment.class);
        List<DyEquipment> equipmentList = builder.where(builder.and(DyEquipmentDao.Properties.AreaId.eq(id),builder.or(DyEquipmentDao.Properties.Type.eq("配电箱"),DyEquipmentDao.Properties.Type.eq("变压器")))).orderAsc(DyEquipmentDao.Properties.CreateTime).list();
        return  equipmentList;
    }

    public void deleteByAreaId(String id){
        List<DyEquipment> items = queryDyEquipmentByAreaId(id);
        for(DyEquipment item :items){
            deleteDyEquipment(item);
        }
    }


    /**
     * 查询全部数据
     */
    public List<DyEquipment> queryAll() {
        return manager.getDaoSession().loadAll(DyEquipment.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<DyEquipment> Equipments = queryAll();
        for(DyEquipment equipment :Equipments){
            jsonArray.add(equipment.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<DyEquipment> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new DyEquipment(jsonArray.getJSONObject(i)));
        }
        insertMultDyEquipment2(list);
    }
}
